package com.jh.notice.business.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.jh.notice.business.SmsSendBusiness;
import com.jh.notice.pojo.SMSRecord;
import com.jh.notice.pojo.SMSRoute;
import com.jh.notice.repository.SmsRepository;
import com.jh.notice.repository.SmsRouteRepository;
import com.jh.notice.util.NoticeConstants;


@Service
public class SmsSendBusinessImpl implements SmsSendBusiness{


	@Autowired
	private SmsRepository smsRepository;

	private static final Logger LOG = LoggerFactory.getLogger(SmsSendBusinessImpl.class);


	@Autowired
	private SmsRouteRepository smsRouteRepository;

	/**分页获取用户的记录*/
	@Override
	public List<SMSRecord> findSmsRecord(Pageable pageable, String phone, Date startTime,  Date endTime) {


		Page<SMSRecord>  result = null;
		if(phone !=null && !phone.equalsIgnoreCase("")){

			if(startTime !=  null){

				if(endTime != null){

					result = smsRepository.findSmsRecordByPhoneStartEndTime(phone, startTime, endTime, pageable);

				}else{

					result = smsRepository.findSmsRecordByPhoneStartTime(phone, startTime, pageable);

				}

			}else{

				result = smsRepository.findSmsRecord(phone,  pageable);

			}

		}else{

			if(startTime !=  null){

				if(endTime != null){

					result = smsRepository.findSmsRecordByStartEndTime(startTime, endTime, pageable);

				}else{

					result=  smsRepository.findSmsRecordByStartTime(startTime, pageable);

				}

			}else{

				result = smsRepository.findAll(pageable);

			}
		}

		return result.getContent();

	}


	/**发送消息*/
	@Override
	public SMSRecord sendSmsMessage(String phone,String tpl_id, Map<String, String> params,String ipAddress,String jhKey,int type) {		//新增type入参

		LOG.info("=====type:" + type);
		SMSRoute smsRoute = smsRouteRepository.getCurActiveSmsChannel();

		SMSRecord  record = new SMSRecord();
		String smsChannel = smsRoute.getCurSMSChannel();

		if(smsChannel.equalsIgnoreCase(NoticeConstants.SMS_CHANNEL_1)){

			JuHeAPISmsService.sendSms(phone,tpl_id, params,jhKey);
		}

		record.setContent(params.get("code"));
		record.setPhone(phone);
		record.setVeriCode(params.get("code"));
		record.setIpAddress(ipAddress);
		record.setCreateTime(new Date());
		record.setType(type);

		/**存短信进入数据库**/
		SMSRecord record2=smsRepository.save(record);
		//DeleteRecordTimer(record2);
		return record2;
	}

	public void DeleteRecordTimer(final SMSRecord record) {			//定时删除验证码方法 付环宇2018.08.13
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				smsRepository.delete(record);
			}
		}, 300000);// 指定延迟300000毫秒后执行（5分钟）
	}

	@Override
	public String querySmscodeByPhone(String phone) {
		SMSRecord smsRecord= smsRepository.findLastestSmsRecord(phone);
		if (smsRecord == null) return null;
		return smsRecord.getVeriCode();
	}

	@Override
	public int querySmscodeTypeByPhone(String phone) {		//新增获取type的方法
		SMSRecord smsRecord= smsRepository.findLastestSmsRecord(phone);
		if (smsRecord == null) return -1;
		return smsRepository.findLastestSmsRecord(phone).getType();
	}

	@Override
	public int findCountByIpAddress(String ipAddress) {
		return smsRepository.findCountByIpAddress(ipAddress,new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
	}


}
